package com.example.warehouse.bus.service.impl;


import com.example.warehouse.bus.dao.persist.repository.IBusCustomerRepository;
import com.example.warehouse.bus.pojo.entity.BusCustomer;
import com.example.warehouse.bus.pojo.param.BusCustomerAddNewParam;
import com.example.warehouse.bus.pojo.vo.BusCustomerStandardVO;
import com.example.warehouse.bus.service.IBusCustomerService;
import com.example.warehouse.common.ex.ServiceException;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.web.ServiceCode;
import com.example.warehouse.sys.pojo.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Linger
 * @date 2023-07-03 11:35
 * @desciption: 处理客户数据的业务的实现类
 */
@Slf4j
@Service
public class BusCustomerServiceImpl implements IBusCustomerService {
    @Value("${warehouse.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IBusCustomerRepository busCustomerRepository;

    public BusCustomerServiceImpl() {
        log.info("创建控制器对象:BusCustomerServiceImpl");
    }

    @Override
    public void addNewBusCustomer(BusCustomerAddNewParam busCustomerAddNewParam) {
        log.debug("开始处理【添加客户】的业务，参数：{}", busCustomerAddNewParam);
        // 检查客户名是否被占用
        {
            String customerName = busCustomerAddNewParam.getCustomerName();
            int count = busCustomerRepository.countByCustomerName(customerName);
            if (count > 0) {
                String message = "添加客户失败，客户名已经被占用！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
        }
        // 检查手机号码是否被占用
        {
            String phone = busCustomerAddNewParam.getPhone();
            int count = busCustomerRepository.countByPhone(phone);
            if (count > 0) {
                String message = "添加客户失败，手机号码已经被占用！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
        }

        // 检查电子邮箱是否被占用
        {
            String email = busCustomerAddNewParam.getEmail();
            int count = busCustomerRepository.countByEmail(email);
            if (count > 0) {
                String message = "添加客户失败，电子邮箱已经被占用！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
        }

        BusCustomer busCustomer = new BusCustomer();
        BeanUtils.copyProperties(busCustomerAddNewParam, busCustomer);
        int rows = busCustomerRepository.insert(busCustomer);
        if (rows != 1) {
            String message = "添加客户失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除客户】的业务，参数：{}", id);
        Object queryResult = busCustomerRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "删除客户失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        log.debug("即将执行删除数据，参数：{}", id);
        int rows = busCustomerRepository.deleteById(id);
        if (rows != 1) {
            String message = "删除客户失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }

    }

    @Override
    public void update(BusCustomer busCustomer) {
        log.debug("开始处理【修改客户】的业务，参数：{}", busCustomer);
        Object queryResult = busCustomerRepository.getStandardById(busCustomer.getId());
        if (queryResult == null) {
            String message = "修改客户失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = busCustomerRepository.update(busCustomer);
        if (rows != 1) {
            String message = "修改客户失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }


    }

    @Override
    public BusCustomerStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询客户】的业务，参数：{}", id);
        return busCustomerRepository.getStandardById(id);
    }

    @Override
    public PageData<BusCustomerStandardVO> queryByKeyWord(Integer pageNum, String customerName, String connectionPerson, String phone) {
        log.debug("开始处理【模糊查询】的业务，参数：{},{},{}", customerName, connectionPerson, phone);
        return busCustomerRepository.queryByKeyWord(pageNum, defaultQueryPageSize,customerName, connectionPerson, phone);
    }

    @Override
    public PageData<BusCustomer> list(Integer pageNum) {
        log.debug("开始处理【查询客户列表】的业务，页码：{}", pageNum);
        PageData<BusCustomer> pageData = busCustomerRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<BusCustomer> listAsc(Integer pageNum) {
        log.debug("开始处理【升序查询客户列表】的业务，页码：{}", pageNum);
        PageData<BusCustomer> pageData = busCustomerRepository.listAsc(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<BusCustomer> listDesc(Integer pageNum) {
        log.debug("开始处理【降序查询客户列表】的业务，页码：{}", pageNum);
        PageData<BusCustomer> pageData = busCustomerRepository.listDesc(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<BusCustomer> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询用户列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<BusCustomer> pageData = busCustomerRepository.list(pageNum, pageSize);
        return pageData;
    }
    @Override
    public void setEnable(Long id) {
        updateEnableById(id, 1);
    }

    @Override
    public void setDisable(Long id) {
        updateEnableById(id, 0);
    }

    private void updateEnableById(Long id, Integer enable) {
        log.debug("开始处理【{}客户】的业务，ID：{}，目标状态：{}", ENABLE_TEXT[enable], id, enable);
         BusCustomerStandardVO queryResult = busCustomerRepository.getStandardById(id);
        if (queryResult == null) {
            String message = ENABLE_TEXT[enable] + "用户失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        if (queryResult.getEnable().equals(enable)) {
            String message = ENABLE_TEXT[enable] + "用户失败，当前用户已经处于"
                    + ENABLE_TEXT[enable] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        BusCustomer user = new BusCustomer();
        user.setId(id);
        user.setEnable(enable);
        log.debug("即将修改数据，参数：{}", user);
        int rows = busCustomerRepository.updateById(user);
        if (rows != 1) {
            String message = ENABLE_TEXT[enable] + "用户失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }


    // @Override
    // public List<BusCustomer> selectAll() {
    //     List<BusCustomer> list = busCustomerRepository.selectAll();
    //     return list;
    // }


}
